package main

import (
	"fmt"
	"sync"
	"time"
)

var (
	m    = make(map[int]int)
	lock sync.Mutex
)

func main() {
	for i := 0; i < 20; i++ {
		go test(i)
	}
	time.Sleep(time.Second * 10)
	//输出结果
	lock.Lock()
	for i, v := range m {
		fmt.Printf("map[%d]=%v\n", i, v)
	}
	lock.Unlock()
}

//阶乘
func test(n int) {
	res := 1
	for i := 1; i <= n; i++ {
		res *= i
	}
	//加锁
	lock.Lock()
	m[n] = res
	//解锁
	lock.Unlock()
}
